package org.csu.mypetstore.controller;

import jakarta.servlet.http.HttpSession;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;
    @Autowired
    private AccountService accountService;

    @GetMapping("/confirmOrder")
    public String confirmOrder(Model model, HttpSession session) {
        Order order = (Order) session.getAttribute("order");
        model.addAttribute("order", order);
        return "order/confirmOrder";
    }

    @GetMapping("/listOrders")
    public String listOrders(HttpSession session, Model model) {
        Order order = (Order) session.getAttribute("order");
        List<Order> orderList = orderService.getOrderList();
        model.addAttribute("order", order);
        model.addAttribute("orderList", orderList);
        return "order/listOrders";
    }

    @GetMapping("/newOrderView")
    public String newOrderView(HttpSession session, Model model) {
        String username = (String)session.getAttribute("username");
        Account account = accountService.getAccount(username);
        Cart cart = cartService.getCartByUsername(username);
        Order order = new Order();
        order.initOrder(account, cart);
        model.addAttribute("order", order);
        session.setAttribute("order", order);
        return "order/newOrder";
    }

    //处理newOrder表单
    @PostMapping("/newOrder")
    public String newOrder(@ModelAttribute Order order, @RequestParam boolean shippingAddressRequired, HttpSession session) {
        Order o = (Order) session.getAttribute("order");
        o.setBillToFirstName(order.getBillToFirstName());
        o.setBillToLastName(order.getBillToLastName());
        o.setBillAddress1(order.getBillAddress1());
        o.setBillAddress2(order.getBillAddress2());
        o.setBillCity(order.getBillCity());
        o.setBillState(order.getBillState());
        o.setBillZip(order.getBillZip());
        o.setBillCountry(order.getBillCountry());
        if(shippingAddressRequired) return "redirect:/order/confirmOrder";
        else return "redirect:/order/shippingView";
    }

    @GetMapping("/shippingView")
    public String shippingView(HttpSession session, Model model) {
        Order order = (Order) session.getAttribute("order");
        model.addAttribute("order", order);
        return "order/shipping";
    }

    //处理shipping表单
    @PostMapping("/shipping")
    public String shipping(@ModelAttribute Order order, HttpSession session) {
        Order o = (Order) session.getAttribute("order");
        o.setShipToFirstName(order.getShipToFirstName());
        o.setShipToLastName(order.getShipToLastName());
        o.setShipAddress1(order.getShipAddress1());
        o.setShipAddress2(order.getShipAddress2());
        o.setShipCity(order.getShipCity());
        o.setShipState(order.getShipState());
        o.setShipZip(order.getShipZip());
        o.setShipCountry(order.getShipCountry());
        return "redirect:/order/confirmOrder";
    }

    @GetMapping("/viewOrder")
    public String viewOrder(HttpSession session, Model model) {
        Order order = (Order) session.getAttribute("order");
        model.addAttribute("order", order);
        orderService.insertOrder(order);
        session.setAttribute("order", null);

        //清空购物车
        String username = (String)session.getAttribute("username");
        Account account = accountService.getAccount(username);
        cartService.removeCart(account.getUsername());
        return "order/viewOrder";
    }
}

